/********************************************************************************
  * @file     SD93F115B_uart.h
  * @author   TEST TEAM 												    	
  * @version  v0															    	
  * @date     Septemper 2022												    	
  * @brief
  *
  * Copyright (C) 2022  Hangzhou SDIC Microelectronics Co., Ltd
**********************************************************************************/

/* Define to prevent recursive inclusion*/
#ifndef __SD93F115B_UART_H
#define __SD93F115B_UART_H


#ifdef __cplusplus
 extern "C" {
#endif

/********Includes***********/
#include "SD93F115B.h"   
   
/************************Exported constants***********************/

/****************UART_Exported_Constants*************************/ 
#ifdef SD93F115B_JQS
#define IS_UART_ALL_PERIPH(PERIPH) (((PERIPH) == UART0)||((PERIPH) == UART1))
#endif
#ifdef SD93F115B_D
#define IS_UART_ALL_PERIPH(PERIPH) (((PERIPH) == UART0)||((PERIPH) == UART1))
#endif
#ifdef SD93F115B_JBS
#define IS_UART_ALL_PERIPH(PERIPH) ((PERIPH) == UART1)
#endif
#ifdef SD93F115B_IMR
#define IS_UART_ALL_PERIPH(PERIPH) ((PERIPH) == UART1)
#endif
/**********************************UART_Word_Length************************/ 
#define UART_WordLength_5b                  ((uint32_t)0x00000000)
#define UART_WordLength_6b                  ((uint32_t)0x00000020)
#define UART_WordLength_7b                  ((uint32_t)0x00000040)
#define UART_WordLength_8b                  ((uint32_t)0x00000060)
#define UART_WordLength_9b                  ((uint32_t)0x00000080)

#define IS_UART_Word_Length(Length) (((Length) == UART_WordLength_5b) || \
									 ((Length) == UART_WordLength_6b) || \
									 ((Length) == UART_WordLength_7b) || \
									 ((Length) == UART_WordLength_8b) || \
									 ((Length) == UART_WordLength_9b))

/*****************************UART_Stop_Bits******************************/ 
#define UART_StopBits_1                     ((uint32_t)0x00000000)
#define UART_StopBits_2                     UART_CR_STOP

#define IS_UART_StopBits(StopBits) (((StopBits) == UART_StopBits_1)||((StopBits) == UART_StopBits_2))  

/**************************UART_Parity************************************/ 
#define UART_Parity_Even                    UART_CR_PCE		                //偶检验
#define UART_Parity_Odd                     (UART_CR_PS|UART_CR_PCE)		//奇检验
#define UART_Parity_None                      ((uint32_t)0x00000000)

#define IS_UART_Parity(Parity) (((Parity) == UART_Parity_Even)||((Parity) == UART_Parity_Odd)||\
								((Parity) == UART_Parity_None))


/*****************************UART_Mode*********************************/ 								 
#define UART_Mode_Rx                         UART_CR_RE
#define UART_Mode_Tx                         UART_CR_TE
#define UART_Mode_TRx                        (UART_CR_RE|UART_CR_TE)

#define IS_UART_Mode(Mode) (((Mode)== UART_Mode_Rx)||((Mode) == UART_Mode_Tx)||((Mode) == UART_Mode_TRx))	

#define UART_CarryWave_PWM0                   UART_CR_CARRY_EN
#define UART_CarryWave_PWM1                   (UART_CR_WAVE_SEL|UART_CR_CARRY_EN)
#define UART_CarryWave_None                   ((uint32_t)0x00000000)

#define IS_UART_CarryWave(Wave) (((Wave)== UART_CarryWave_PWM0)||((Wave) == UART_CarryWave_PWM1)||((Parity) == UART_CarryWave_None))							 

/********************UART_Interrupt_definition*****************************/
#define UART_IT_RXNEIE                       UART_CR_RXNEIE
#define UART_IT_TXEIE                        UART_CR_TXEIE
#define UART_IT_TCIE                         UART_CR_TCIE
#define UART_IT_PEIE                         UART_CR_PEIE
#define UART_IT_EIE                          UART_CR_EIE
#define UART_IT_WKUPIE                       UART_CR_WKUPIE

#define IS_UART_ITSource(IT) (((IT) == UART_IT_RXNEIE)||((IT) == UART_IT_TXEIE)||\
							  ((IT) == UART_IT_TCIE)||((IT) == UART_IT_FEIE)||\
							  ((IT) == UART_IT_EIE)||((IT) == UART_IT_WKUPIE))

/***************************UART_Flags*************************************/
#define UART_Flag_RXNE                       UART_SR_RXNE 
#define UART_Flag_TXE                        UART_SR_TXE
#define UART_Flag_TC                         UART_SR_TC
#define UART_Flag_FE                         UART_SR_FE
#define UART_Flag_ORE                        UART_SR_ORE
#define UART_Flag_NE                         UART_SR_NE
#define UART_Flag_PE                         UART_SR_PE
#define UART_Flag_WKUPF                      UART_SR_WKUPF
#define UART_Flag_ABRE                       UART_SR_ABRE

#define IS_UART_StatusFlag(Flag) (((Flag) == UART_Flag_RXNE)||((Flag) == UART_Flag_TXE)||\
							      ((Flag) == UART_Flag_TC)||((Flag) == UART_Flag_FE)||\
							      ((Flag) == UART_Flag_ORE)||((Flag) == UART_Flag_NE)||\
							      ((Flag) == UART_Flag_PE)||((Flag) == UART_Flag_WKUPF)\
								  ((Flag) == UART_Flag_ABRE)) 
								  
#define IS_UART_ClearFlag(Flag)  (((Flag) == UART_Flag_TC)||((Flag) == UART_Flag_FE)||\
							      ((Flag) == UART_Flag_ORE)||((Flag) == UART_Flag_NE)||\
							      ((Flag) == UART_Flag_PE)||((Flag) == UART_Flag_WKUPF)\
								  ((Flag) == UART_Flag_ABRE)) 

/**********************UART_Global_definition***************************************/
#define IS_UART_BuadRate(BuadRate) (((BuadRate)>=0) && ((BuadRate)>=0x7FFF))     //              
#define IS_UART_Data(Data) ((Data) <= 0x1FF)

/**********UART Init Structure definition***************/ 
typedef struct
{
  uint32_t UART_BaudRate;                  //设置UART波特率     
	
  uint32_t UART_WordLength;                //设置UART的传输字长

  uint32_t UART_StopBits;                  //设置UART的停止位          

  uint32_t UART_Parity;                    //设置UART的奇偶校验位
 
  uint32_t UART_CarryWave;                 //设置UART的载波功能          
 
  uint32_t UART_Mode;                      //设置UART的通信模式，如单发送、单接收、既发送又接收       

} UART_InitTypeDef;

#define UART1_IOMAP_P36_P37          (uint8_t)0x00
#define	UART1_IOMAP_SWCLK_SWDIO      (uint8_t)0x01

#define IS_UART_UART1_IOMAP(UART1_IOMAP) (((UART1_IOMAP)==UART1_IOMAP_P36_P37)||((UART1_IOMAP)==UART1_IOMAP_SWCLK_SWDIO))

/* Exported macro ------------------------------------------------------------*/
/* Exported functions ------------------------------------------------------- */

/* Initialization and Configuration functions *********************************/
void UART_DeInit(UART_TypeDef* UARTx);                                                     //将UART相关寄存器恢复为上电默认值

void UART_Init(UART_TypeDef* UARTx, UART_InitTypeDef* UART_InitStruct);                    //初始化UART相关参数
void UART_StructInit(UART_InitTypeDef* UART_InitStruct);                                   //初始化UART_InitTypeDef,即给结构体赋初值
void UART_Cmd(UART_TypeDef* UARTx, FunctionalState NewState);                              //使能UART功能

uint32_t UART_GetSYSCLK(void);                                                             //获取当前系统时钟值，便于波特率的计算
uint32_t UART_GetClock(UART_TypeDef* UARTx);                                               //获取UART的时钟值，便于波特率的计算


void UART_AutoBaudRateCmd(UART_TypeDef* UARTx, FunctionalState NewState);                  //设置UART自动波特率检测功能


uint16_t UART_ReceiveData(UART_TypeDef* UARTx);                                            //接收数据
void UART_SendData(UART_TypeDef* UARTx, uint16_t Data);                                    //发送数据

/********************Interrupts and flags management functions ******************************/

void UART_ITConfig(UART_TypeDef* UARTx, uint32_t UART_IT, FunctionalState NewState);       //使能UART相关中断功能

FlagStatus UART_GetFlagStatus(UART_TypeDef* UARTx, uint32_t UART_Flag);                    //获取UART状态寄存器的相关标志

void UART_ClearFlagStatus(UART_TypeDef* UARTx, uint32_t UART_Flag);                        //清零UART状态寄存器的相关标志位

void UART_UART1_IOMAP(uint8_t UART1_IOMAP);                                                //UART1通信接口映射选择

#ifdef __cplusplus
}
#endif

#endif /* __SD93F115B_UART_H */


/*****************************END OF FILE********************************/